<?php

class ReportsController extends AppController {

    var $name = "Reports";
    var $uses = array('UserRoleMenu', 'Menu', "Report", 'Chart', 'ChartCategory');
    var $layout = "report_layout";
    public $components = array('Pdf', 'Upload');
    public $helpers = array('DateFormat');
    var $paginate = array(
        'order' => array(
            'Report.id' => 'asc'
        )
    );

    public function admin_index() {
        $this->layout = "default";
        
        $user = $this->Session->read('user');

        $user_menus = $this->UserRoleMenu->find('list', array(
            'fields' => array("menu_id"),
            'conditions' => array('user_role_id' => $user['User']['user_role_id']),
            'recursive' => -1)
        );

        $menus = $this->Menu->find('all', array(
            'contain' => array('ParentMenu'),
            'conditions' => array(
                'Menu.id' => $user_menus, 'Menu.level' => 3, 'ParentMenu.title' => 'Reports'
            ),
            'recursive' => 1,
            'order' => array('Menu.position'))
        );

        $this->set('menus', $menus);
    }
    
    public function admin_reports() {
        
    }
    
    public function admin_charts() {
        $this->layout = "default";
        
        $chart_categories = $this->ChartCategory->find('all', array(
            'contain' => array(
                'Chart' => array('order' => array('title'))
            )
        ));
        $this->set('chart_categories', $chart_categories);
    }

    public function admin_reportlist() {

        $report_menus = $this->Report->find("all");


        $this->set(compact('report_menus'));
    }

    public function admin_del($report_id) {

        $conditions = array("Report.id" => $report_id);
        $this->Report->deleteAll($conditions, false);
        echo json_encode(array("status" => true));
        exit;
    }

    //this is used for adding new reports
    public function admin_add($report_id=null) {

        if ($report_id != null) {

            $report_data = $this->Report->find("first", array("conditions" => array("Report.id" => $report_id)));
            $this->set(compact('report_data'));
        }

        if (isset($_POST['data'])) {
            $data = $_POST['data']['Report'];
            $this->Report->save($data);
            echo json_encode(array("status" => "true"));
            exit();
        }
    }

    //this is for loading the real report
    public function admin_vreport($report_id=null, $type=null) {
        $this->layout = "print_layout";
        $query = $this->Report->find("first", array("conditions" => array("Report.id" => $report_id)));
        $columns = array();
        $keys = array();
        $title = $query['Report']['title'];
        $results = $this->Report->query($query['Report']['params']);
        if (count($results) > 0) {

            foreach ($results[0] as $val) {
                foreach ($val as $key => $keyval) {

                    $columns[] = $key;
                }
            }
        }


        $this->set(compact('results', 'columns', 'title'));
    }

    public function admin_test_page() {

        //$this->autoLayout=false;
        $this->autoRender = false;
        $data = $this->paginate('Permit');
        print_r($data);
    }

    public function admin_dreport($report_id) {

        //
       
             $this->autoLayout = false;
            $this->autoRender = false;
            $limit = 25;
            $query = $this->Report->find("first", array("limit" => null, "conditions" => array("Report.id" => $report_id)));
            $columns = array();
            $keys = array();
            $title = $query['Report']['title'];
            $results = $this->Report->query($query['Report']['params']);
            if (count($results) > 0) {

                foreach ($results[0] as $val) {
                    foreach ($val as $key => $keyval) {

                        $columns[] = $key;
                    }
                }
            }

           

           $this->Pdf->download($title."pdf", '/reports/admin_dreport', array("limit" => $limit, "title" => $title, "report_id" => $report_id, 'results' => $results, 'columns' => $columns));
            return;
            
    }

    //will have to write a function tommorow which will merge all the existing functionlity and put 
   // 
    
    public function admin_report($report_id=null, $limit=null, $start=null, $end=null) {
        $limit = 25;
        $query = $this->Report->find("first", array("limit" => $limit, "conditions" => array("Report.id" => $report_id)));
        $columns = array();
        $keys = array();
        $title = $query['Report']['title'];
        $results = $this->Report->query($query['Report']['params']);
        if (count($results) > 0) {

            foreach ($results[0] as $val) {
                foreach ($val as $key => $keyval) {

                    $columns[] = $key;
                }
            }
        }


        $this->set(compact('results', 'columns', 'title', 'report_id', 'limit'));
    }

    //this is for loading hte main view report
    public function admin_viewreport($report_id=null) {

        $this->set(compact('report_id'));
    }

    //for viewing reports
    public function admin_printreport() {
        
    }

    //for downloading reports
    public function admin_download_report() {
        
    }

    //for emailing reports
    public function admin_email_report() {
        
    }

}

?>
